package com.l2jserver.gameserver.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javolution.util.FastMap;

import com.l2jserver.L2DatabaseFactory;
import com.l2jserver.gameserver.model.actor.FakePlayer;

/**
 * Fake Player data table.
 * @author fissban
 */
public class FakePlayerTable
{
	private static Logger _log = Logger.getLogger(FakePlayerTable.class.getName());
	
	private final FastMap<Integer, FakePlayer> _fakePcs = new FastMap<>();
	
	private FakePlayerTable()
	{
	}
	
	/**
	 * Cargamos desde la DB todos los datos para construir un FakePlayer
	 */
	public void loadData()
	{
		_fakePcs.clear();
		
		try (Connection con = L2DatabaseFactory.getInstance().getConnection();
			PreparedStatement stmt = con.prepareStatement("SELECT * FROM fake_pcs");
			ResultSet rset = stmt.executeQuery())
		{
			
			while (rset.next())
			{
				FakePlayer fpc = new FakePlayer();
				
				final int npcId = rset.getInt("npc_id");
				fpc.set_name(rset.getString("name"));
				fpc.set_sex(rset.getInt("sex"));
				fpc.set_class(rset.getInt("class"));
				
				fpc.set_pdHelmet(rset.getInt("pd_helmet"));
				fpc.set_pdHead(rset.getInt("pd_head"));
				fpc.set_pdRHand(rset.getInt("pd_rhand"));
				fpc.set_pdLHand(rset.getInt("pd_lhand"));
				fpc.set_pdGloves(rset.getInt("pd_gloves"));
				fpc.set_pdChest(rset.getInt("pd_chest"));
				fpc.set_pdLegs(rset.getInt("pd_legs"));
				fpc.set_pdFeet(rset.getInt("pd_feet"));
				fpc.set_pdCloak(rset.getInt("pd_cloak"));
				fpc.set_pdHair(rset.getInt("pd_hair"));
				fpc.set_pdHair2(rset.getInt("pd_hair2"));
				
				fpc.set_pdRHandAug(rset.getInt("pd_rhand_aug"));
				fpc.set_pdLHandAug(rset.getInt("pd_lhand_aug"));
				
				fpc.set_hairStyle(rset.getInt("hair_style"));
				fpc.set_hairColor(rset.getInt("hair_color"));
				fpc.set_face(rset.getInt("face"));
				
				fpc.set_title(rset.getString("title"));
				
				fpc.set_clanId(rset.getInt("clan_id"));
				fpc.set_clanCrestId(rset.getInt("clan_crest_id"));
				fpc.set_allyId(rset.getInt("ally_id"));
				fpc.set_allyCrestId(rset.getInt("ally_crest_id"));
				
				fpc.set_sitWhileIdle(rset.getInt("sit_while_idle"));
				
				fpc.set_mount(rset.getInt("mount"));
				fpc.set_mountNpcId(rset.getInt("mount_npc_id"));
				
				fpc.set_team(rset.getInt("team"));
				
				fpc.set_clanCrestLargeId(rset.getInt("clan_crest_large_id"));
				
				fpc.set_hero(rset.getInt("hero"));
				
				fpc.set_fishing(rset.getInt("fishing"));
				fpc.set_fishingX(rset.getInt("fishing_x"));
				fpc.set_fishingY(rset.getInt("fishing_y"));
				fpc.set_fishingZ(rset.getInt("fishing_z"));
				
				fpc.set_pledgeClass(rset.getInt("pledge_class"));
				fpc.set_pledgeType(rset.getInt("pledge_type"));
				
				fpc.set_nameColor(Integer.decode("0x" + rset.getString("name_color")));
				fpc.set_titleColor(Integer.decode("0x" + rset.getString("title_color")));
				
				fpc.set_agathionId(rset.getInt("agathion_id"));
				
				_fakePcs.put(npcId, fpc);
			}
			
			_log.log(Level.INFO, "FakePlayerTable: Loaded " + _fakePcs.size() + " FakePlayers.");
		}
		catch (SQLException sqlex)
		{
			_log.log(Level.WARNING, "Failed to load fake players table!", sqlex);
		}
	}
	
	/**
	 * Volvemos a cargar los FakePlayers
	 */
	public void reloadData()
	{
		loadData();
	}
	
	public FakePlayer getFake(int npcId)
	{
		return _fakePcs.get(npcId);
	}
	
	public static FakePlayerTable getInstance()
	{
		return SingletonHolder._instance;
	}
	
	@SuppressWarnings("synthetic-access")
	private static class SingletonHolder
	{
		protected static final FakePlayerTable _instance = new FakePlayerTable();
	}
}